window.onload=function(){
	if(!document.getElementsByClassName){
		document.getElementsByClassName=function(cls){
			var ret=[];
			var els=document.getElementsByTagName('*');
			for( var i=0;i<els.length;i++){
				if(els[i].className=cls
				||els[i].className.indexof(cls+" ")
				||els[i].className.indexOf(' '+cls+' ')
				||els[i].className.indexOf(' '+cls)){
					ret.push(els[i]);
				}
			}
			return ret;
		}
	}
	
var table_1=document.getElementById("table-1");
var checks=document.getElementsByClassName('check');
var tr=table_1.children[1].rows;
var num=document.getElementsByClassName('count-input');		
var add=document.getElementsByClassName('add');
var reduce=document.getElementsByClassName('reduce');
var input=document.getElementsByTagName('input');
var selecttotal=document.getElementById("selectTotal");
var priceTotal=document.getElementById('priceTotal');
var quanxuan=document.getElementsByClassName('quanxuan');
var del=document.getElementsByClassName('delete');
var tbody=document.getElementById("tbody");
var shanchu=document.getElementById("shanchu");
var checkones=document.getElementsByClassName('check-one');
var view=document.getElementById("view");
var selected=document.getElementsByClassName('selected')[0];
var spans=view.getElementsByTagName('span');

for(var i in checks){
	checks[i].checked=true;
}

for (var i = 0; i < add.length; i++) {//点击add事件
	    add[i].id=i;
		add[i].onclick=function(){
			this.previousElementSibling.value++;
			if(this.previousElementSibling.value>=1){
				reduce[this.id].innerText='-';
			}
			change();
		}
	}	

for (var i = 0; i < reduce.length; i++) { //点击reduce事件
	reduce[i].onclick=function(){
		if(this.nextElementSibling.value>1){
		   this.nextElementSibling.value--;
		   change();
		  }
		   if(this.nextElementSibling.value<=1){
			this.innerText='';
		}
	}
}

for (var i = 0; i < quanxuan.length; i++) {
	quanxuan[i].onclick=function(){
		var check=this.checked;
		for(var i in checks){
			checks[i].checked=check;
		}
	}
}

for (var i = 0; i < checkones.length; i++) {
	checkones[i].onchange=function(){
		change();
		getchecked();
		goodlist();
	}
}

for (var i = 0; i < del.length; i++) {//删除单行
	del[i].onclick=function(){
		var conf=confirm('确定要删除吗？');
		if(conf){
		tbody.removeChild(this.parentNode.parentNode);
	   	change();
	   	goodlist();
		}
	}
}
shanchu.onclick=function(){
	if(selecttotal.innerText!=0){
	    var conf=confirm('确定要删除吗？');
		if(conf){
			for (var i = 0; i < checkones.length; i++) {
				if(checkones[i].checked){
					tbody.removeChild(checkones[i].parentNode.parentNode);
					i--;
					change();
					goodlist();
				}
			}
		}
	}
}
function change(){
	var count=0;
	var sum=0;
	for (var i = 0; i < checkones.length; i++) {
		if(checkones[i].checked){
		 tr[i].cells[4].innerText=(parseFloat(tr[i].cells[2].innerText)*parseInt(num[i].value)).toFixed(2);
		 count += parseInt(num[i].value) ;
		 sum +=parseFloat (tr[i].cells[4].innerText);
		
		}
	}
	  selecttotal.innerText=count;
	  priceTotal.innerText=sum.toFixed(2);
}
change();	
	
selected.onclick=function(){
	if(view.style.display=='none'){
		view.style.display='block';
		goodlist();
	}else{
		view.style.display='none';
	}
}
function goodlist(){
	var str='';
	for (var i = 0; i < tr.length; i++) {
		if(checkones[i].checked){
		str += '<div><img src="'+ tr[i].getElementsByTagName('img')[0].src+'"><span index="'+i+'">取消选择</span></div>';
		}
	}
	view.innerHTML=str;
}


view.onclick=function(e){
	var e=e||window.event;
	var el=e.target||e.srcElement;
	if(el.tagName.toLowerCase()=='span'){
		view.removeChild(el.parentNode);
		tr[el.getAttribute('index')].getElementsByClassName('check')[0].checked=false;
		change();
	}
}

function getchecked(){
	var num=0;
	for (var i = 0; i < checkones.length; i++) {
		if(checkones[i].checked){
			num++;
		}
	}
	for(var j=0;j< quanxuan.length;j++){
		if(num==tr.length ){
			quanxuan[j].checked=true;
	   }else{
	   	quanxuan[j].checked=false;
	   }
	}   	
	
}
	
}
